package com.intellekt.ecert.connection.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.simple.ParameterizedRowMapper;

import com.intellekt.ecert.connection.vo.ConnectionVO;
import com.intellekt.ecert.domain.CatalogoVO;
import com.intellekt.ecert.domain.JdbcDao;

public class ConnectionsDaoImpl extends JdbcDao implements ConnectionsDao {

	private String qryConnections;
	private String qryCountries;
	private String qryConnectionExist;
	
	
	private static ParameterizedRowMapper<CatalogoVO> MAPPER_CATALOGO = new ParameterizedRowMapper<CatalogoVO>() {
		public CatalogoVO mapRow(ResultSet rs, int rowNum) throws SQLException {
			CatalogoVO obj = new CatalogoVO();
			obj.setKeyTxt(rs.getString("KEY"));
			obj.setValue(rs.getString("VALUE"));

			return obj;
		}
	};
	
	private static ParameterizedRowMapper<ConnectionVO> MAPPER_CONNECTION = new ParameterizedRowMapper<ConnectionVO>() {
		public ConnectionVO mapRow(ResultSet rs, int rowNum) throws SQLException {
			ConnectionVO obj = new ConnectionVO();
			
			obj.setIdConnection(rs.getString("CVE_CONEXION"));
			obj.setIdCountry(rs.getString("CVE_PAIS"));
			obj.setDescCountry(rs.getString("PA.DES_PAIS"));
			obj.setUsername(rs.getString("USERNAME"));
			obj.setPassword(rs.getString("PASSWORD"));
			obj.setUrl(rs.getString("URL"));
			obj.setStatus(rs.getString("CVE_ESTATUS").equals("A")? true:false);
			
			return obj;
		}
	};
	
	@Override
	public List<CatalogoVO> getCountries() {
		if (log.isDebugEnabled()) {
			log.debug("getCountries...");
		}
		return (List<CatalogoVO>) jdbcTemplate.query(qryCountries, MAPPER_CATALOGO);
	}
	
	
	@Override
	public List<ConnectionVO> getConnections() {
		if (log.isDebugEnabled()) {
			log.debug("getConnections...");
		}
		return (List<ConnectionVO>) jdbcTemplate.query(qryConnections, MAPPER_CONNECTION);
	}

	@Override
	public boolean connectionExist(String idConnection) {
		if (log.isDebugEnabled()) {
			log.debug("getCountries...");
		}
		String params[] = {idConnection};
		
		String a = jdbcTemplate.queryForObject(qryConnectionExist, params, String.class);
		if(a.equals("1")){
			return true;
		}else{
			return false;
		}
	}

	
	public void setQryConnections(String qryConnections) {
		this.qryConnections = qryConnections;
	}
	public void setQryCountries(String qryCountries) {
		this.qryCountries = qryCountries;
	}
	public void setQryConnectionExist(String qryConnectionExist) {
		this.qryConnectionExist = qryConnectionExist;
	}
}
